#include "components/TimeSequencePanel.h"

#include <string>

using models::Process;
using models::TimePoint;

struct DrawLine {
  std::string pid;
  int x1;
  int x2;
  int y;
  int line_type; // 1: solid, 2: dot
};

namespace components {
TimeSequencePanel::TimeSequencePanel(wxWindow *parent,
                                     const std::vector<Process> &processes,
                                     const std::vector<TimePoint> &time_points)
    : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
              wxBORDER_SIMPLE) {
  m_time_points = time_points;
  m_processes = processes;
  m_painter = new Painter(processes, time_points);

  wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
  m_imageCtrl = new wxStaticBitmap(this, wxID_ANY, wxNullBitmap);
  wxButton *btnPlotAll = new wxButton(this, wxID_ANY, "Plot Full");
  wxButton *btnPlotNextStep = new wxButton(this, wxID_ANY, "Next Step");
  wxButton *btnExportVideo = new wxButton(this, wxID_ANY, "Export Video");

  sizer->Add(m_imageCtrl, 1, wxEXPAND | wxALL, 5);
  sizer->Add(btnPlotAll, 0, wxALIGN_CENTER | wxALL, 5);
  sizer->Add(btnPlotNextStep, 0, wxALIGN_CENTER | wxALL, 5);
  sizer->Add(btnExportVideo, 0, wxALIGN_CENTER | wxALL, 5);

  SetSizer(sizer);

  btnPlotAll->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) {
    wxImage image = m_painter->plot_all();
    m_imageCtrl->SetBitmap(wxBitmap(image));
    Refresh();
  });

  btnPlotNextStep->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) {
    wxImage image = m_painter->plot_next_step();
    m_imageCtrl->SetBitmap(wxBitmap(image));
    Refresh();
  });

  btnExportVideo->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) {
    wxFileDialog saveFileDialog(this, _("Save Video File"), "", "",
                                "MP4 files (*.mp4)|*.mp4|All files (*.*)|*.*",
                                wxFD_SAVE | wxFD_OVERWRITE_PROMPT);

    if (saveFileDialog.ShowModal() == wxID_CANCEL) {
      return;
    }
    wxString filePath = saveFileDialog.GetPath();
    m_painter->export_video(filePath.ToStdString());
  });

  Bind(wxEVT_PAINT, &TimeSequencePanel::OnPaint, this);
}

void TimeSequencePanel::OnPaint(wxPaintEvent &event) {
  // 这里不再创建新的控件
  wxPaintDC dc(this);
  // 其他绘制操作
}
} // namespace components